package com.stu.controller;

import com.stu.commons.Result;
import com.stu.commons.ResultCode;
import com.stu.pojo.ProductCheck;
import com.stu.service.IProductCheckService;
import com.stu.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品审核控制器
 * @author zy
 * @date 2025/5/26
 */
@RestController
@RequestMapping("/productCheck")
public class ProductCheckController {

    @Autowired
    private IProductCheckService productCheckService;

    /**
     * 根据商品ID获取商品审核记录
     */
    @GetMapping("/{productId}")
    public Result<ProductCheck> getById(@PathVariable Integer productId) {
        ProductCheck product = productCheckService.getById(productId);
        return product != null ?
                Result.success(product) :
                Result.fail(ResultCode.SERVER_ERROR, "商品不存在");
    }

    /**
     * 新增商品（设置默认商家 ID 为 1008）
     */
    @PostMapping("/add")
    public Result<String> addProduct(@RequestBody ProductCheck productCheck,
                                     HttpServletRequest request) {
        int userId = 1008;  // 测试用， 改为实际登录用户 ID

        String token = request.getHeader("Authorization").substring(7);
        String userIdrel = JwtUtil.getUserIdFromToken(token);
        userId = Integer.parseInt(userIdrel);

        int rows = productCheckService.addProduct(productCheck, userId);
        return rows > 0 ?
                Result.success("新增商品成功") :
                Result.fail(ResultCode.SERVER_ERROR, "新增商品失败");
    }

    /**
     * 获取审核通过的商品列表
     */
    @GetMapping("/pass/{userId}")
    public Result<List<ProductCheck>> getCheckedPass(@PathVariable Integer userId,
                                                     HttpServletRequest request) {
        String token = request.getHeader("Authorization").substring(7);
        String userIdrel = JwtUtil.getUserIdFromToken(token);
        userId = Integer.parseInt(userIdrel);
//        System.out.println("userIdrel: " + userIdrel);

        List<ProductCheck> list = productCheckService.getCheckedPass(userId);
        return list != null && !list.isEmpty() ?
                Result.success(list) :
                Result.fail(ResultCode.SERVER_ERROR, "未找到审核通过的商品");
    }

    /**
     * 获取审核不通过的商品列表
     */
    @GetMapping("/fail/{userId}")
    public Result<List<ProductCheck>> getCheckedFail(@PathVariable Integer userId,
                                                     HttpServletRequest request) {
        String token = request.getHeader("Authorization").substring(7);
        String userIdrel = JwtUtil.getUserIdFromToken(token);
        userId = Integer.parseInt(userIdrel);

        List<ProductCheck> list = productCheckService.getCheckedFail(userId);
        return list != null && !list.isEmpty() ?
                Result.success(list) :
                Result.fail(ResultCode.SERVER_ERROR, "未找到审核失败的商品");
    }

    /**
     * 获取未审核的商品列表
     */
    @GetMapping("/unchecked/{userId}")
    public Result<List<ProductCheck>> getUnChecked(@PathVariable Integer userId,
                                                   HttpServletRequest request) {
        String token = request.getHeader("Authorization").substring(7);
        String userIdrel = JwtUtil.getUserIdFromToken(token);
        userId = Integer.parseInt(userIdrel);

        List<ProductCheck> list = productCheckService.getUnChecked(userId);
        return list != null && !list.isEmpty() ?
                Result.success(list) :
                Result.fail(ResultCode.SERVER_ERROR, "未找到未审核的商品");
    }
    // TODO: 下面是管理员操作
    /**
     * 获取所有待审核商品列表
     */
    @GetMapping("/admin/all")
    public Result<List<ProductCheck>> getAllChecked(HttpServletRequest request) {
        String token = request.getHeader("Authorization").substring(7);
        String userId = JwtUtil.getUserIdFromToken(token);
        List<ProductCheck> list = productCheckService.getAllChecked();
        return list != null && !list.isEmpty() ?
                Result.success(list) :
                Result.fail(ResultCode.SERVER_ERROR, "未找到商品");
    }

    /**
     * 获取所有已审核通过的商品列表
     */
    @GetMapping("/admin/allCheckedPass")
    public Result<List<ProductCheck>> getAllCheckedPass(HttpServletRequest request) {
        String token = request.getHeader("Authorization").substring(7);
        String userId = JwtUtil.getUserIdFromToken(token);
        List<ProductCheck> list = productCheckService.getAllCheckedPass(userId);
        return list != null && !list.isEmpty() ?
                Result.success(list) :
                Result.fail(ResultCode.SERVER_ERROR, "未找到已审核通过的商品");
    }

    /**
     * 获取所有已审核不通过的商品列表
     */
    @GetMapping("/admin/allCheckedFail")
    public Result<List<ProductCheck>> getAllCheckedFail(HttpServletRequest request) {
        String token = request.getHeader("Authorization").substring(7);
        String userId = JwtUtil.getUserIdFromToken(token);
        List<ProductCheck> list = productCheckService.getAllCheckedFail(userId);
        return list != null && !list.isEmpty() ?
                Result.success(list) :
                Result.fail(ResultCode.SERVER_ERROR, "未找到已审核不通过的商品");
    }

    /**
     * 审核通过商品
     */
    @PutMapping("/checkPass/{productId}")
    public Result<String> checkPass(@PathVariable Integer productId,
                                     HttpServletRequest request) {
//        int userId = 1008;  // 测试用， 改为实际登录用户 ID
        String token = request.getHeader("Authorization").substring(7);
        String userIdrel = JwtUtil.getUserIdFromToken(token);
        int userId = Integer.parseInt(userIdrel);

        int rows = productCheckService.checkPass(productId, userId);
        return rows > 0 ?
                Result.success("审核通过成功") :
                Result.fail(ResultCode.SERVER_ERROR, "审核操作失败");
    }
    /**
     * 审核不通过商品
     */
    @PutMapping("/checkFail/{productId}")
    public Result<String> checkFail(@PathVariable Integer productId,
                                     HttpServletRequest request) {
//        int userId = 1008;  // 测试用， 改为实际登录用户 ID
        String token = request.getHeader("Authorization").substring(7);
        String userIdrel = JwtUtil.getUserIdFromToken(token);
        int userId = Integer.parseInt(userIdrel);

        int rows = productCheckService.checkFail(productId, userId);
        return rows > 0 ?
                Result.success("审核不通过成功") :
                Result.fail(ResultCode.SERVER_ERROR, "审核操作失败");
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/delete/{productId}")
    public Result<String> deleteProduct(@PathVariable Integer productId) {
        int rows = productCheckService.deleteProduct(productId);
        return rows > 0 ?
                Result.success("删除成功") :
                Result.fail(ResultCode.SERVER_ERROR, "删除失败");
    }
}
